using System;
using System.Collections.Generic;
using SimpleJSON;
using UnityEngine;

namespace Gj.DarkLoong
{
    public class Map
    {
        public static void CustomCreate(string title, string desc, string type, string category, JSONObject dataD, JSONArray tags, string version, Action<JSONObject, string> callback)
        {
            if (!Auth.IsLogin()) return;
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"custom create: {title} {type} {dataD}");
                var data = new JSONObject();
                data["title"] = title;
                data["type"] = type;
                data["classes"] = category;
                data["desc"] = desc;
                if (tags != null)data["tags"] = tags;
                data["app_version"] = version;
                data["data"] = dataD;
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Post($"/workshop/map/custom", data,
                    header);
                Api.AutoResult(response, callback);
            });
        }

        public static void CustomConfirm(string no, Action<bool, string> callback)
        {
            if (!Auth.IsLogin()) return;
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"custom confirm: {no}");
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Post($"/workshop/map/custom/{no}/confirm", new JSONObject(),
                    header);
                Api.AutoResult(response, callback);
            });
        }
        public static void CustomUpdate(string no, string title, string desc, JSONObject dataD, JSONArray tags, string version, bool resetLeaderboard, Action<bool, string> callback)
        {
            if (!Auth.IsLogin()) return;
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"custom update: {no} {title} {desc} {resetLeaderboard}");
                var data = new JSONObject();
                data["title"] = title;
                data["desc"] = desc;
                if (tags != null)data["tags"] = tags;
                data["app_version"] = version;
                data["data"] = dataD;
                data["resetLeaderboard"] = resetLeaderboard;
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Post($"/workshop/map/custom/{no}", data,
                    header);
                Api.AutoResult(response, callback);
            });
        }
        public static void CustomSave(string no, string title, string desc, Action<bool, string> callback)
        {
            if (!Auth.IsLogin()) return;
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"custom update: {no} {title} {desc}");
                var data = new JSONObject();
                data["title"] = title;
                data["desc"] = desc;
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Post($"/workshop/map/custom/{no}/base", data,
                    header);
                Api.AutoResult(response, callback);
            });
        }
        public static void UpdateMap(string no, JSONArray tags, Action<bool, string> callback)
        {
            // if (!Auth.IsLogin()) return;
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                var data = new JSONObject();
                if (tags != null)data["tags"] = tags;
                var header = Api.CookieHeader();
                // header["token"] = Auth.SessionToken;
                var response = await Api.Post($"/workshop/map/custom/{no}/super", data,
                    header);
                Api.AutoResult(response, callback);
            });
        }
        public static void CustomDelete(string no, Action<bool, string> callback)
        {
            if (!Auth.IsLogin()) return;
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"custom delete: {no}");
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Delete($"/workshop/map/custom/{no}", new JSONObject(),
                    header);
                Api.AutoResult(response, callback);
            });
        }

        public static void CustomList(string category, string type, bool news, bool hot, bool my, bool collect, bool recommend, bool follow, JSONArray tags, int page, int size,
            Action<JSONObject, string> callback)
        {
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"custom list: {news} {hot} {my} {collect} {recommend} {page} {size}");
                var data = new JSONObject();
                data["classes"] = category;
                data["type"] = type;
                data["news"] = news;
                data["hot"] = hot;
                data["my"] = my;
                data["collect"] = collect;
                data["recommend"] = recommend;
                data["follow"] = follow;
                if (tags != null) data["tags"] = tags;
                data["page"] = page;
                data["size"] = size;
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Get($"/workshop/map/custom/list", data,
                    header);
                Api.AutoResult(response, callback);
            });
        }

        public static void CustomListByType(string listType, string category, string type, int page, int size,
            Action<JSONObject, string> callback)
        {
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"custom list: {type} {page} {size}");
                var data = new JSONObject();
                data["list_type"] = listType;
                data["classes"] = category;
                data["type"] = type;
                data["page"] = page;
                data["size"] = size;
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Get($"/workshop/map/custom/list", data,
                    header);
                Api.AutoResult(response, callback);
            });
        }

        public static void CustomLike(string no, string action, Action<bool, string> callback)
        {
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"custom like: {no} {action}");
                var data = new JSONObject();
                data["action"] = action;
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Put($"/workshop/map/custom/{no}/like", data,
                    header);
                Api.AutoResult(response, callback);
            });
        }

        public static void CustomCollect(string no, string action, Action<bool, string> callback)
        {
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"custom collect: {no} {action}");
                var data = new JSONObject();
                data["action"] = action;
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Put($"/workshop/map/custom/{no}/collect", data,
                    header);
                Api.AutoResult(response, callback);
            });
        }

        public static void GetCustomRecord(string no, Action<JSONObject, string> callback)
        {
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"custom GetCustomRecord: {no}");
                var data = new JSONObject();
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Get($"/workshop/map/custom/{no}", data,
                    header);
                Api.AutoResult(response, callback);
            });
        }

        public static void SaveCustomRecord(string no, string room, string e, JSONObject meta, Action<bool, string> callback)
        {
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"custom SaveCustomRecord: {no} {room} {e} {meta}");
                var data = new JSONObject();
                data["meta"] = meta;
                data["room"] = room;
                data["event"] = e;
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Put($"/workshop/map/custom/{no}/record", data,
                    header);
                Api.AutoResult(response, callback);
            });
        }

        public static void Feedback(string no, JSONArray answer, Action<bool, string> callback)
        {
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"custom Feedback: {no} {answer}");
                var data = new JSONObject();
                data["answer"] = answer;
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Post($"/workshop/map/custom/{no}/feedback", data,
                    header);
                Api.AutoResult(response, callback);
            });
        }

        public static void Event(string category, string no, string e, JSONObject meta, Action<bool, string> callback)
        {
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"map event: {category} {no} {e} {meta}");
                var data = new JSONObject();
                data["meta"] = meta;
                data["event"] = e;
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Put($"/workshop/map/{category}/{no}/event", data,
                    header);
                Api.AutoResult(response, callback);
            });
        }
    }
}